package com.act.moudles.baseAct.controller;


import com.act.moudles.baseAct.entity.TaskAnoVars;
import com.act.moudles.baseAct.service.TaskAnoVarsService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.act.utils.validations.AddGroup;
import com.act.utils.validations.UpdateGroup;
import com.act.utils.validations.ValidResult;
import com.act.utils.validations.ValidationUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("baseAct/taskAnoVars")
@Api(tags = "流程节点变量")
public class TaskAnoVarsController {

    @Resource
    private TaskAnoVarsService taskAnoVarsService;

    @PostMapping("page")
    @ApiOperation("分页查询")
    public DataResult page(@RequestBody PageSearch<TaskAnoVars> pageSearch){
        IPage<TaskAnoVars>  ipage = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("查询成功！",taskAnoVarsService.page(ipage,pageSearch.getDomain().query()));
    }

    @PostMapping("addVars")
    @ApiOperation("新增变量")
    public DataResult addVars(@RequestBody TaskAnoVars taskAnoVars){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(taskAnoVars, AddGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        String addRes = taskAnoVarsService.addVars(taskAnoVars);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("新增成功！");
    }

    @PostMapping("updVars")
    @ApiOperation("修改变量")
    public DataResult updVars(@RequestBody TaskAnoVars taskAnoVars){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(taskAnoVars, UpdateGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        String addRes = taskAnoVarsService.updVars(taskAnoVars);
        if(StringUtils.isNotEmpty(addRes)){
            return DataResult.fail(addRes);
        }
        return DataResult.success("修改成功！");
    }

    @GetMapping("removeVars")
    @ApiOperation("删除")
    public DataResult removeVars(Long id){
        taskAnoVarsService.removeById(id);
        return DataResult.success("删除成功！");
    }



}
